<?php
declare (strict_types = 1);
namespace app\common;

use think\Request;

class RequestExt extends Request
{
    /**
     * 是否过滤Input中的数组值
     * @var bool
     */
    protected $filterArray = true;

    protected function filterData($data, $filter, $name, $default)
    {
        if ($this->filterArray) {
            // 解析过滤器
            $filter = $this->getFilter($filter, $default);
            if (!is_array($data)) {
                $this->filterValue($data, $name, $filter);
                return $data;
            }
            if (!empty($name)) {
                return $default;
            }
            foreach ($data as $k => &$v) {
                if (is_array($v)) {
                    $data[$k] = $default;
                    continue;
                }
                $this->filterValue($v, $k, $filter);
            }
            return $data;
        } else {
            return parent::filterData($data, $filter, $name, $default);
        }
    }

    public function setFilterArrayStatus(bool $status = true)
    {
        $this->filterArray = $status;
    }
}
